﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
//using Oracle.DataAccess.Client;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using trangbaocao.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;
using System.Data;

namespace trangbaocao.Models
{
    public class Main_StationsModel
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Tên trạm không được để trống!")]
        public string name { get; set; }
        public string dia_chi { get; set; }
        public string quan_ly_tt { get; set; }
        public string so_dien_thoai { get; set; }
        public string ghi_chu { get; set; }

        public List<Main_StationsModel> getMain_Stations()
        {
            List<Main_StationsModel> Main_Stations = new List<Main_StationsModel>();
            string sql="";
            if(HttpContext.Current.Session!=null)
            {
                int quyen_id = Convert.ToInt16(HttpContext.Current.Session["quyen_id"]);
                int trungtam_id = Convert.ToInt16(HttpContext.Current.Session["trungtam_id"]);
                if (quyen_id > 2)
                    sql = "SELECT * FROM main_stations where id="+trungtam_id+" order by name"; 
                else
                    sql = "SELECT * FROM main_stations order by name";
            }
            else
                sql = "SELECT * FROM main_stations order by name";
            
            OracleDataReader odr = General.Lib_GetDataReader(sql);
            while (odr.Read())
            {
                Main_Stations.Add(new Main_StationsModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), dia_chi = odr[2].ToString(), quan_ly_tt = odr[3].ToString(), so_dien_thoai = odr[4].ToString(), ghi_chu = odr[5].ToString() });
            }
            return Main_Stations;
        }
        public DataSet bc_Chungloaithietbi()
        {
            string sql = "select a.name,count(b.id) tong_so_tram from main_stations a,stations b where a.id=b.main_stations_id group by a.name order by a.name";
            OracleConnection conn = General.ConnectDataBase();
            OracleCommand omd = new OracleCommand(sql, conn);
            OracleDataAdapter oda = new OracleDataAdapter(omd);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
            }
            catch
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Lỗi kết nối với cơ sở dữ liệu!')</SCRIPT>");
            }
            oda.Fill(ds, "0");
            conn.Close();
            return ds;
        }
    }
}